package com.hyzhangjr.webdemo;

import org.springframework.boot.Banner;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;

import java.util.Arrays;

@SpringBootApplication
public class WebDemoApplication  extends SpringBootServletInitializer {

	public static void main(String[] args) {
		SpringApplication.run(WebDemoApplication.class, args);
	}

	/*需要在容器中运行时生成war包，启动类继承SpringBootServletInitializer并重写configure方法*/
	@Override
	protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
		return configureApplication(builder);
	}

	private static SpringApplicationBuilder configureApplication(SpringApplicationBuilder builder) {
		return builder.sources(WebDemoApplication.class).bannerMode(Banner.Mode.OFF);
	}


	/*容器启动后自动调用*/
	@Bean
	public CommandLineRunner commandLineRunner(ApplicationContext ctx){
		return args -> {
			System.out.println("Let's inspect the beans provided by Spring Boot:");
			String[] beansNames = ctx.getBeanDefinitionNames();
			Arrays.sort(beansNames);
			for(String name:beansNames){
				System.out.println(name);
			}
		};
	}
}
